//
//  PetAmuseViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "PetAmuseViewController.h"
#import "ZCXActivityView.h"
#import "ZCXActivityViewController.h"
#import "ZCXVideoViewController.h"
#import "ZCXPetAmuseCollectionViewCell.h"
#import "ZCXNewPetVideoDetialViewController.h"
#import "ZCXPerPictureViewController.h"

#import "ZCXPetGifViewController.h"

#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface PetAmuseViewController ()< UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectView;
@property (nonatomic, retain) NSArray *array;
@property (nonatomic, retain) NSArray *titleArray;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, retain) UIScrollView *bigScrollView;
@end

@implementation PetAmuseViewController
- (void)dealloc{
    [_collectView release];
    [_array release];
    [_titleArray release];
    [_bigScrollView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"萌宠搞笑";
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.array = @[@"Zgou1.jpg", @"Zmiao1.jpg", @"Ztu1.jpeg", @"Zxiyi1.jpg", @"Zmiao3.jpg", @"Zyazi.jpg", @"Zshe.jpg", @"Zhouzi.jpg", @"Zlaoshu.jpg", @"Zyingwu.jpg", @"Zwugui.jpg", @"Zyinyu.jpg"];
    self.titleArray = @[@"宠物狗", @"宠物猫", @"宠物兔子", @"宠物蜥蜴", @"宠物龙猫", @"宠物鸭子", @"宠物蛇", @"宠物猴子", @"宠物鼠", @"宠物鹦鹉", @"宠物乌龟", @"宠物金鱼"];
    self.bigScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 34 * HEIGHT)];
    _bigScrollView.contentSize = CGSizeMake(0, self.view.frame.size.height * 1);
    _bigScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_bigScrollView];
    [_bigScrollView release];
    [self LoopView];
    [self createCollectionView];
}
- (void)LoopView{
    NSArray *picArray = @[@"Zcatt.jpg", @"Zcat2.jpg", @"Zcat4.jpg", @"Ztutu.jpg"];
    NSArray *themeArray = @[@"萌宠图片", @"萌宠乐园", @"萌宠话题", @"萌宠GIF"];
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 230 * HEIGHT)];
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, 0);
    scrollView.tag = 2000;
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    for (int i = 0; i < 4; i ++) {
        ZCXActivityView *picView = [[ZCXActivityView alloc]initWithFrame:CGRectMake(i * self.view.frame.size.width , 0, self.view.frame.size.width, 230 * HEIGHT)];
        picView.imageV.image = [UIImage imageNamed:picArray[i]];
        picView.tag = 1000 + i;
        picView.titleLabel.text = themeArray[i];
        [picView addTarget:self action:@selector(picViewAction:)];
        [scrollView addSubview:picView];
        [picView release];
    }
    [_bigScrollView addSubview:scrollView];
    [scrollView release];
    UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(80 * WIDTH, scrollView.frame.size.height - 50 * HEIGHT, 200 * WIDTH, 30 * HEIGHT)];
    page.tag = 1500;
    page.currentPage = 1;
    page.numberOfPages = 4;
    [self.bigScrollView addSubview:page];
    [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(changePictures) userInfo:nil repeats:YES];
    [page release];
}

- (void)createCollectionView{
    //collectionView
    self.automaticallyAdjustsScrollViewInsets = NO;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height / 2 - 80 * HEIGHT - 44 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height / 2 + 80 * HEIGHT) collectionViewLayout:flowLayout];
    
    _collectView.delegate = self;
    _collectView.dataSource = self;
    _collectView.bounces = NO;
    _collectView.pagingEnabled = YES;
    _collectView.backgroundColor = [UIColor whiteColor];
    //cell注册
    [self.collectView registerClass:[ZCXPetAmuseCollectionViewCell class] forCellWithReuseIdentifier:@"ZCXcollectionViewCell"];
    [self.bigScrollView addSubview:_collectView];
    [flowLayout release];
    [_collectView release];
}
- (void)picViewAction:(ZCXActivityView *)sender{
    switch (sender.tag) {
        case 1000:{
            ZCXPerPictureViewController *petPicture = [[ZCXPerPictureViewController alloc]init];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:petPicture animated:YES];
            self.hidesBottomBarWhenPushed = NO;
            [petPicture release];
            break;
        }
        case 1001:{
            ZCXVideoViewController *video = [[ZCXVideoViewController alloc]init];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:video animated:YES];
            self.hidesBottomBarWhenPushed = NO;
            [video release];
            break;
        }
        case 1002:{
            ZCXActivityViewController *activity = [[ZCXActivityViewController alloc]init];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:activity animated:YES];
            self.hidesBottomBarWhenPushed = NO;
            [activity release];
            break;
        }
        case 1003:{
            ZCXPetGifViewController *gif = [[ZCXPetGifViewController alloc]init];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:gif animated:YES];
            self.hidesBottomBarWhenPushed = NO;
            [gif release];
            break;
        }
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    UIPageControl *page = [self.view viewWithTag:1500];
    page.currentPage = scrollView.contentOffset.x / self.view.frame.size.width;
}
-(void)changePictures{
    UIScrollView *scrollView = [self.view viewWithTag:2000];
    [scrollView setContentOffset:CGPointMake(self.view.frame.size.width * self.number, 0) animated:YES];
    UIPageControl *page = [self.view viewWithTag:1500];
    page.currentPage = self.number;
    self.number++;
    if (self.number >= 4) {
        self.number = 0;
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 12;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZCXPetAmuseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZCXcollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor purpleColor];
    cell.imageView.image = [UIImage imageNamed:self.array[indexPath.row]];
    cell.label.text = self.titleArray[indexPath.row];
        return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.view.frame.size.width / 3 - 8 * WIDTH, self.view.frame.size.height / 7);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1 * WIDTH;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1 * HEIGHT;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(5 * HEIGHT, 6 * WIDTH, 5 * HEIGHT, 5 * WIDTH);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ZCXNewPetVideoDetialViewController * newPet = [[ZCXNewPetVideoDetialViewController alloc]init];
    newPet.titleName = self.titleArray[indexPath.row];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:newPet animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    [newPet release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
